package com.zjg.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_adminer;
import com.common.entity.A_complain;
import com.common.utils.StringUtils;
import com.zjg.dao.ComplainDao;
import com.zjg.dao.impl.ComplainDaoImpl;

public class ComplainServlet extends HttpServlet {
	private ComplainDao complainDao=new ComplainDaoImpl();
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String opr=request.getParameter("opr");
		
		if("update".equals(opr))
		{
			update(request,resp);
		}
		else if("del".equals(opr)){
			delete(request, resp);
		}else{
			list(request, resp);
		}
	}
	
	private void list(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		Map<String, Object> map=new HashMap<String, Object>();
		Integer is_solve=StringUtils.str2Integer(request.getParameter("is_solve"), null);
		if(is_solve!=null)
		{
			map.put("is_solve", is_solve);
		}
		List<A_complain> list=complainDao.findByCondition(map);
		request.setAttribute("complainlist", list);
		String is_excute_complain=(String) request.getSession().getAttribute("is_excute_complain");
		if(is_excute_complain!=null&&is_excute_complain.equals("yes"))
		{
			request.getSession().setAttribute("is_excute_complain", "no");
			request.getRequestDispatcher("complain_list.jsp").forward(request, resp);
		}
		else
		{
			request.getSession().setAttribute("excuteMsg_complain", "");
			request.getRequestDispatcher("complain_list.jsp").forward(request, resp);
		}
	}
	
	private void update(HttpServletRequest request, HttpServletResponse resp)
			throws UnsupportedEncodingException, IOException {
		//2、判断数据的有效性
		//3、获取数据
		A_adminer adminer=(A_adminer) request.getSession().getAttribute("userInfo");
		int rows=0;
		String errMsg="";
		try{
			Integer complain_id=StringUtils.str2Integer(request.getParameter("complain_id"), null);
			A_complain complain=null;
			if(complain_id!=null){
				complain=complainDao.findById(complain_id);
				if(complain==null) throw new RuntimeException("找不到该记录："+complain_id);
				//4、保存数据
				complain.setIs_solve(StringUtils.str2Integer(request.getParameter("is_solve"), complain.getIs_solve()));
				complain.setAdmin_id(adminer.getAdmin_id());
				complain.setResults(request.getParameter("results"));
				complain.setEnd_date(StringUtils.str2TimestampBySql(request.getParameter("end_date"), null, new Timestamp(new Date().getTime()), false));
				rows=complainDao.updateComplain(complain);
			}else{
				throw new RuntimeException("找不到该记录");
			}
		}catch(Exception e){
			e.printStackTrace();
			errMsg=e.getMessage();
		}
		if(rows>0){
			request.getSession().setAttribute("is_excute_complain", "yes");
			request.getSession().setAttribute("excuteMsg_complain", "修改成功");
		}else{
			request.getSession().setAttribute("excuteMsg_complain", errMsg);
		}
		resp.sendRedirect("complainServlet");
	}
	
	
	private void delete(HttpServletRequest request, HttpServletResponse resp)
			throws IOException {
		int rows=0;
		String errMsg="";
		String del_list=request.getParameter("del_list");
		if(del_list!=null&&del_list.length()>0)
		{
			String[] delArrStr=del_list.split(";");
			List<Integer> delArrInt=new ArrayList<Integer>();
			for(int i=0;i<delArrStr.length;i++)
			{
				delArrInt.add(StringUtils.str2Integer(delArrStr[i], null));
			}
			for(Integer i:delArrInt)
			{
				rows=complainDao.deleteComplaine(i);
			}
		}
		else{
			try{
				Integer complain_id=StringUtils.str2Integer(request.getParameter("complain_id"), null);
				if(complain_id!=null){
					//4、保存数据
					rows=complainDao.deleteComplaine(complain_id);
				}else{
					throw new RuntimeException("该记录不存在");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg=e.getMessage();
			}
		}
		if(rows>0){
			request.getSession().setAttribute("is_excute_complain", "yes");
			request.getSession().setAttribute("excuteMsg_complain", "删除成功");
		}else{
			request.getSession().setAttribute("excuteMsg_complain", errMsg);
		}
		resp.sendRedirect("complainServlet");
	}
}
